/*
 * Copyright (c) 田梓萱 2021-2022.
 * All Rights Reserved.
 * All codes are protected by China's regulations on the protection of computer software, and infringement must be investigated.
 * 版权所有 (c) 田梓萱 2021-2022.
 * 所有代码均受中国《计算机软件保护条例》保护，本项目已获得软著.
 */

package com.xcl.calculator.slice.BianQian.second;

import com.xcl.calculator.ResourceTable;
import com.xcl.calculator.XLog;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.app.Context;
import ohos.distributedhardware.devicemanager.DeviceInfo;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

/**
 * The type Contact component 1.
 *
 * @author Xcl
 * @date 2021 /1/12
 * @package com.xcl.calculator.slice.BianQian.second
 */
class ContactComponent1 extends DirectionalLayout {
    /**
     * The constant label.
     */
    private static final HiLogLabel label = new HiLogLabel(HiLog.LOG_APP, 0x00234, "ContactComponent1");
    /**
     * The constant NAME_FLAG.
     */
    private static final int NAME_FLAG = 1;

    /**
     * The Dialog component.
     */
    private Component dialogComponent;

    /**
     * The Titles text field.
     */
    private TextField titlesTextField;

    /**
     * The Messages text field.
     */
    private TextField messagesTextField;

    /**
     * The Title.
     */
    private Text title;

    /**
     * The Dialog call back.
     */
    private DialogCallBack dialogCallBack;

    /**
     * 实例化
     *
     * @param context the context
     * @param b       the b
     */
    ContactComponent1(Context context, int b) {
        super(context);
        this.addComponent(context, b);
        this.initView();
        this.initEvent();
    }

    /**
     * 初始化
     */
    private void initEvent() {
        try {
            this.dialogComponent.findComponentById(ResourceTable.Id_confirm).setClickedListener(new ClickedListener() {
                @Override
                public void onClick(Component component) {
                    ContactComponent1.this.confirmContact();
                }
            });
        } catch (Exception e) {
            XLog.error(label, e.getMessage());
        }
    }

    /**
     * 其它
     */
    private void confirmContact() {
        try {
            String titlesInput = this.titlesTextField.getText();
            String messagesInput = this.messagesTextField.getText();
            if (this.dialogCallBack != null) this.dialogCallBack.result(titlesInput, messagesInput);
        } catch (Exception e) {
            XLog.error(label, e.getMessage());
        }
    }

    /**
     * 初始化
     */
    private void initView() {
        try {
            Component componentText = this.dialogComponent.findComponentById(ResourceTable.Id_titles);
            if (componentText instanceof TextField) this.titlesTextField = (TextField) componentText;
            componentText = this.dialogComponent.findComponentById(ResourceTable.Id_messages);
            if (componentText instanceof TextField) this.messagesTextField = (TextField) componentText;
            componentText = this.dialogComponent.findComponentById(ResourceTable.Id_title);
            if (componentText instanceof Text) this.title = (Text) componentText;
        } catch (Exception e) {
            XLog.error(label, e.getMessage());
        }
    }

    /**
     * 其它
     *
     * @param context the context
     * @param a       the a
     */
    private void addComponent(Context context, int a) {
        try {
            if (a == DeviceInfo.DeviceType.PHONE.value()) {
                this.dialogComponent = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_bianqian_2m,
                        null, false);
                XLog.info(label, "当前设备类型为手机");
            } else if (a == DeviceInfo.DeviceType.TABLET.value()) {
                this.dialogComponent = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_bianqian_2t,
                        null, false);
                XLog.info(label, "当前设备类型为平板");
            } else XLog.warn(label, "暂不支持您的设备:(" + a + ")");
            this.addComponent(this.dialogComponent);
            LayoutConfig layoutConfig = new LayoutConfig(LayoutConfig.MATCH_PARENT, LayoutConfig.MATCH_CONTENT);
            this.setLayoutConfig(layoutConfig);
        } catch (Exception e) {
            XLog.error(label, e.getMessage());
        }
    }

    /**
     * 初始化数据
     *
     * @param titles   the titles
     * @param messages the messages
     */
    void initData(String titles, String messages) {
        try {
            this.titlesTextField.setBubbleSize(0, 0);
            this.messagesTextField.setBubbleSize(0, 0);
            if (titles != null) this.titlesTextField.setText(titles);
            if (messages != null) {
                this.messagesTextField.setText(messages);
                this.title.setText("修改信息");
            }
        } catch (Exception e) {
            XLog.error(label, e.getMessage());
        }
    }

    /**
     * 其它
     *
     * @param flag the flag
     */
    void setTextFiledErrorGraphic(int flag) {
        try {
            ShapeElement errorElement = new ShapeElement(this.getContext(), ResourceTable.Graphic_background_text_field_error);
            this.titlesTextField.clearFocus();
            this.messagesTextField.clearFocus();
            if (NAME_FLAG == flag) this.titlesTextField.setBackground(errorElement);
            else
                this.messagesTextField.setBackground(errorElement);
        } catch (Exception e) {
            XLog.error(label, e.getMessage());
        }
    }

    /**
     * 其它
     */
    void setTextFiledNormalGraphic() {
        try {
            ShapeElement normalElement = new ShapeElement(this.getContext(), ResourceTable.Graphic_background_input_bq);
            this.titlesTextField.setBackground(normalElement);
            this.messagesTextField.setBackground(normalElement);
        } catch (Exception e) {
            XLog.error(label, e.getMessage());
        }
    }

    /**
     * 其它
     *
     * @param dialogCallBack the dialog call back
     */
    void setDialogCallBack(DialogCallBack dialogCallBack) {
        this.dialogCallBack = dialogCallBack;
    }

    /**
     * 其它
     */
    public interface DialogCallBack {
        /**
         * Result.
         *
         * @param titles   the titles
         * @param messages the messages
         */
        void result(String titles, String messages);
    }
}
